/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.loopj.hos.image.slice;

import com.loopj.hos.image.ResourceTable;
import com.loopj.hos.image.adapter.ListAdapter;
import com.loopj.hos.image.adapter.ViewHolder;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.window.service.WindowManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.sysappcomponents.contact.ContactsCollection;
import ohos.sysappcomponents.contact.entity.Contact;
import ohos.sysappcomponents.contact.entity.PhoneNumber;

import java.util.ArrayList;
import java.util.List;

/**
 * author zhaoxudong
 * Version 1.0
 * ModifiedBy
 * date 2021-04-14 10:13
 * description 联系人列表
 */
public class AddressSelectAbilitySlice extends AbilitySlice {
    private static final HiLogLabel HI_LOG_LABEL = new HiLogLabel(HiLog.LOG_APP, 0xFFFFF, "MainAbilitySlice");
    private ListContainer listContainer;
    private ListAdapter listAdapter;
    private ContactsCollection contactsCollection;
    private List<Contact> contactList;

    public AddressSelectAbilitySlice(ContactsCollection collection) {
        contactsCollection = collection;
    }

    @Override
    public void onStart(Intent intent) {
        // 设置状态栏颜色
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(getColor(ResourceTable.Color_purple_700));
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_address);
        initView();
    }

    private void initView() {
        contactList = new ArrayList<>();
        while (!contactsCollection.isEmpty()) {
            Contact contact = contactsCollection.next();
            contactList.add(contact);
        }
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_address);
        listContainer.enableScrollBar(1, true);
        listAdapter = new ListAdapter<Contact>(getAbility(), ResourceTable.Layout_text_item, contactList) {
            @Override
            public void convert(ViewHolder viewHolder, Contact contact, int position) {
                Text text = viewHolder.getView(ResourceTable.Id_number_text);
                if (null == contact){
                    HiLog.debug(HI_LOG_LABEL, "initView contact is null");
                    return;
                }
                if (null != contact.getName()){
                    text.setText(contact.getName().getFullName());
                }else {
                    List<PhoneNumber> phoneNumberList = contact.getPhoneNumbers();
                    text.setText(phoneNumberList.size() > 0 ? phoneNumberList.get(0).getPhoneNumber() : "");
                }
                Image headerImage = viewHolder.getView(ResourceTable.Id_header);
                if (position == 0) {
                    headerImage.setPixelMap(ResourceTable.Media_1);
                } else if (position == 1) {
                    headerImage.setPixelMap(ResourceTable.Media_2);
                } else if (position == 2) {
                    headerImage.setPixelMap(ResourceTable.Media_3);
                } else if (position == 3) {
                    headerImage.setPixelMap(ResourceTable.Media_4);
                } else {
                    headerImage.setPixelMap(ResourceTable.Media_4);
                }
            }
        };
        listContainer.setItemProvider(listAdapter);
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int position, long l) {
                Intent intent = new Intent();
                intent.setParam("addressId", position);
                setResult(intent);
                terminate();
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
